package com.bit.spring.controller;

import com.bit.spring.entity.Person;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

@RequestMapping("/return")
//@RestController
//@ResponseBody
@Controller
public class ReturnController {

    @RequestMapping("/returnPage")
    public String returnPage(){
        return "/index.html";
    }

    @ResponseBody
    @RequestMapping("/returnData")
    public String returnData(){
        return "/index.html";
    }

    @ResponseBody
    @RequestMapping("/returnHTML")
    public String returnHTML(){
        return "<h1>returnHTML</h1>";
    }

    @ResponseBody
    @RequestMapping("/returnJSON")
    public Person returnJSON(){
        Person person = new Person();
        person.setName("zhangsan");
        person.setAge(12);
        person.setPassword("123456");
        return person;
    }

    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response){
        response.setStatus(502);
        return "设置状态码成功";
    }

    @ResponseBody
    @RequestMapping("/setHeader")
    public String setHeader(HttpServletResponse response){
        response.setHeader("My-Header", "MyHeader");
        return "设置Header成功";
    }


    @ResponseBody
    @RequestMapping("/setContentType")
    public void setContentType(HttpServletResponse response) throws IOException {
        response.setContentType("text/html");//纯文本  text/plain
        response.setCharacterEncoding("utf-8");
        response.getOutputStream().write("<h1>returnHTML</h1>".getBytes(StandardCharsets.UTF_8));
    }

    @ResponseBody
    @RequestMapping(value = "/setContentType2", produces = "text/plain")
    public String setContentType2(){
        return "<h1>returnHTML</h1>";
    }


    @ResponseBody
    @RequestMapping(value = "/setContentType3", produces = "application/json")
    public String setContentType3(){
        return "{\"OK\": 1}";
    }



}
